<template>

	<view v-if="isShow">
		<view class="updatetips-whole">
			<view class="updatetips">
				<view>
					<view class="updatetips-head">
						发现新版本V{{versionNum}}
					</view>
					<view class="updatetips-content">
						<view class="" v-for="(item,index) in versionContent">
							{{item}}
						</view>
					</view>

				</view>
				<view class="updatetips-btn-wrap">
					<view class="updatetips-btn-refuse" @click="isShow=false">取消</view>
					<view class="updatetips-btn" @click="downloadBtn()">立即更新</view>
				</view>

			</view>
		</view>
	</view>

</template>

<script>
	import NotifiUtil from './notification'

	export default {
		props: {
			show: {
				type: Boolean,
				default: false
			},
			versionNum: {
				type: String,
				default: ''
			},
			versionContent: {
				type: Array,
				default: () => {}
			},
			downloadUrl: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				isShow: this.show,
			};
		},
		methods: {
			downloadBtn() {
				if (uni.getSystemInfoSync().osName == 'ios') {
					plus.runtime.launchApplication({
						action: `itms-apps://itunes.apple.com/cn/app/id6443766257?mt=8`
					}, function(e) {
						console.log('Open system default browser failed: ' + e.message);
					})
				}

				if (uni.getSystemInfoSync().osName == 'android') {
					this.isShow = false
					var appname = '松鼠云采'
					var NotificationUtil = NotifiUtil(); //实例创建
					NotificationUtil.setNotification(appname, "开始下载! ")
					var dtask = plus.downloader.createDownload(this.downloadUrl)
					dtask.start();

					var arr = [{
							pro: 1,
							isFirst: true
						}, {
							pro: 10,
							isFirst: true
						},
						{
							pro: 30,
							isFirst: true
						}, {
							pro: 50,
							isFirst: true
						},
						{
							pro: 70,
							isFirst: true
						}, {
							pro: 90,
							isFirst: true
						}
					]

					dtask.addEventListener("statechanged", async function(task, status) {
						switch (task.state) {
							case undefined: //下载任务未开始
							case 0: //下载任务开始调度
							case 1: //下载任务开始请求
							case 2:
								break; //下载任务请求已经接收
							case 3: // 已接收到数据
								var pro = parseInt(task.downloadedSize / task.totalSize * 100);
								switch (pro) { //优化方案二：仅进度第一次出现（1,10，30，50，70，90）时刷新
									case arr[0].pro:
										if (arr[0].isFirst) {
											console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[0].isFirst = false;
										}
										break;
									case arr[1].pro:
										if (arr[1].isFirst) {
											console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[1].isFirst = false;
										}
										break;
									case arr[2].pro:
										if (arr[2].isFirst) {
											console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[2].isFirst = false;
										}
										break;
									case arr[3].pro:
										if (arr[3].isFirst) {
											console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[3].isFirst = false;
										}
										break;
									case arr[4].pro:
										if (arr[4].isFirst) {
											console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[4].isFirst = false;
										}
										break;
									case arr[5].pro:
										if (arr[5].isFirst) {
											// console.log("进度：" + pro);
											NotificationUtil.setProgress(pro, appname);
											arr[5].isFirst = false;
										}
										break;
									default:
										break;
								}
								break;
							case 4: // 下载完成
								console.log("Download success: " + task.filename);
								NotificationUtil.compProgressNotification(appname, "下载完成! ");
								plus.runtime.install(task.filename); // 安装下载的apk文件  
								break;
							default: //5: (Number 类型 )下载任务已暂停 -1: (Number 类型 )枚举任务状态
								console.log("Download failed: " + status);
								NotificationUtil.compProgressNotification(appname, "下载失败! ");
								break;
						}
					})
				}

			},
		}
	}
</script>



<style scoped lang="scss">
	.updatetips-whole {
		position: fixed;
		top: 0;
		z-index: 200;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.updatetips {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 80%;
		background-color: #fff;
		border-radius: 20rpx;
	}

	.updatetips-head {
		text-align: center;
		font-size: 40rpx;
		width: 100%;
		height: 50rpx;
		padding: 20rpx 0;
	}

	.updatetips-content {
		line-height: 1.2;
		padding: 0 30rpx;

		view {
			margin: 10rpx 0;
		}
	}

	.updatetips-btn-wrap {
		border-top: 10rpx #f5f5f5 solid;
		display: flex;

		>view {
			width: 50%;
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
		}
	}

	.updatetips-btn-refuse {
		border-right: 5rpx #f5f5f5 solid;
	}

	.updatetips-btn {
		border-left: 5rpx #f5f5f5 solid;
		color: #1376f7;
	}
</style>